package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.EmpChartQueryObject;
import cn.wolfcode.crm.service.IMarketService;
import cn.wolfcode.crm.util.Dictionary;
import cn.wolfcode.crm.util.JSONUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/12.
 */

@Controller
@RequestMapping("market")
public class MarketController {

    @Autowired
    private IMarketService marketService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"market:view", "考勤页面"}, logical = Logical.OR)
    public String view() {
        return "market";
    }


    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"task:query", "考勤列表"}, logical = Logical.OR)
    public List<Map<String,Object>> query(EmpChartQueryObject qo) {
        return marketService.query(qo);
    }



    @RequestMapping("saleByBar")
    @RequiresPermissions(value = {"market:saleByBar", "考勤页面"}, logical = Logical.OR)
    public String saleByBar(Model model,@ModelAttribute("qo") EmpChartQueryObject qo) {
        System.out.println(qo.getGroupType());
        //创建数组封装图表接收的数组格式 x, y
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();

        //迭代出每个groupType与totalAmount的值分装到指定的数组中
        List<Map<String, Object>> maps = marketService.query(qo);
        for (Map<String, Object> map : maps) {
            System.out.println(map);
            x.add(map.get("groupType"));
            y.add(map.get("totalAmount"));
        }
        //获取页面传来的groupTypt做回显
        model.addAttribute("groupType", Dictionary.SALE_MAP.get(qo.getGroupType()));
        //设置共享数据并把数组转成json格式返回到页面
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("y", JSONUtil.toJSONString(y));
        return "saleByBar";
    }


    @RequestMapping("saleByPic")
    @RequiresPermissions(value = {"market:saleByPic", "考勤页面"}, logical = Logical.OR)
    public String saleByPic(Model model,@ModelAttribute("qo") EmpChartQueryObject qo) {
        //创建数组封装图表接收的数组格式 x, datas
        List<Object> x = new ArrayList<>();//["admin","will"]
        List<Object> datas = new ArrayList<>();//[name="admin",value="111"]

        //迭代出每个groupType与totalAmount的值分装到指定的数组中
        List<Map<String, Object>> maps = marketService.query(qo);
        for (Map<String, Object> map : maps) {
            x.add(map.get("groupType"));
            Map<String,Object> ky = new HashMap<>();
            ky.put("name",map.get("groupType"));
            ky.put("value",map.get("totalAmount"));
            datas.add(ky);
            System.out.println(ky);//{name=admin, value=1667400.00}
            System.out.println(datas);//[{name=admin, value=1667400.00}, {name=李四明, value=288000.00}]
        }
        //获取页面传来的groupTypt做回显
        model.addAttribute("groupType", Dictionary.SALE_MAP.get(qo.getGroupType()));
        //设置共享数据并把数组转成json格式返回到页面
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("datas", JSONUtil.toJSONString(datas));
        return "saleByPic";
    }

}